package com.sx.pipeline.ui.needs.construction.approve.frag

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.app.base.CYDataBindingBaseFragment
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.FragReleaseNeedsBinding
import com.sx.pipeline.databinding.FragReleaseTaskBinding
import com.sx.pipeline.repos.http.res.ReleaseNeedsVO
import com.sx.pipeline.ui.needs.construction.approve.ApprovalNeedsVM
import com.sx.pipeline.ui.needs.construction.release.frag.ReleaseNeedsListAdapter


/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description:
 */
class ApprovalNeedsFragment :
    CYDataBindingBaseFragment<FragReleaseNeedsBinding, ApprovalNeedsVM>(R.layout.frag_release_needs),View.OnClickListener {
    private lateinit var mAdapter: ApprovalNeedsListAdapter
    private var status = 0 //需求状态

    companion object {
        fun newInstance(position: Int) =
            ApprovalNeedsFragment().apply {
                arguments = Bundle().apply {
                    putInt(IntentKey.ID, position)
                }
            }
    }

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        status = arguments?.getInt(IntentKey.ID)!!
        mViewModel.auditNeedPage(status, mPage)
    }

    override fun initListener() {
        mBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {

            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mPage++
                mViewModel.auditNeedPage(status)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mPage = 1
                mViewModel.auditNeedPage(status)
            }
        })
    }


    override fun initViewObservable() {
        super.initViewObservable()

        // 刷新事件
        observe(mViewModel.mUI.initRefreshEvent()) {
            mBinding.refreshLayout.finishRefresh(it!!)
        }

        // 加载事件
        observe(mViewModel.mUI.initLoadEvent()) {
            mBinding.refreshLayout.finishLoadMore(it!!)
        }

        //操作成功
        observe(mViewModel.auditSuccessEvent){
            mPage=1
            mViewModel.auditNeedPage(status, mPage)
        }

        // 需求列表列表
        setNeedsList()
    }

    private fun setNeedsList() {
        observe(mViewModel.approvalNeedsListEvent) {
            if (it != null && !it.records.isNullOrEmpty()) {
                mBinding.emptyView.visibility = View.GONE
                mBinding.recyclerView.visibility = View.VISIBLE
                if (!this::mAdapter.isInitialized) {
                    mBinding.recyclerView.layoutManager = LinearLayoutManager(
                        context, LinearLayoutManager.VERTICAL, false
                    )
                    mAdapter =
                        ApprovalNeedsListAdapter(it.records as MutableList<ReleaseNeedsVO>)
                    mBinding.recyclerView.adapter = mAdapter
                    mAdapter.setOnItemClickListener { adapter, view, position ->
                        val info = adapter.data[position] as ReleaseNeedsVO
                        ARouter.getInstance()
                            .build(RoutePath.Mine.CONSTRUCTION_APPROVAL_NEEDS_DETAIL)
                            .withInt(IntentKey.ID,info.id)
                            .navigation()
                    }
                    //拒绝任务
                    mAdapter.rejectRequireCallback = { id: Int ->
                        XPopup.Builder(context)
                            .autoOpenSoftInput(true)
                            .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                            .asInputConfirm(
                                getString(R.string.txt_please_input_reject_reason),
                                null,
                                "",
                                getString(R.string.txt_please_input),
                                {
                                    if (TextUtils.isEmpty(it)) {
                                        ToastUtils.showShort("请输入拒绝原因")
                                        return@asInputConfirm
                                    }
                                    mViewModel.getNeedOrderAudit(id,it,2)
                                },
                                null,
                                R.layout.dialog_task_reject
                            ) //最后一个参数绑定已有布局
                            .show()
                    }
                    //同意任务
                    mAdapter.agreeRequireCallback = {id: Int->
                        mViewModel.getNeedOrderAudit(id,"",1)
                    }


                } else {
                    mBinding.refreshLayout.setEnableLoadMore(true)
                    if (mPage == 1) {
                        mAdapter.setNewInstance(it.records as MutableList<ReleaseNeedsVO>)
                    } else {
                        mAdapter.addData(it.records)
                    }
                }
                if (mAdapter.data.size == it.total) {
                    mBinding.refreshLayout.setEnableLoadMore(false)
                }
            } else {
                mBinding.refreshLayout.setEnableLoadMore(false)
                mBinding.emptyView.visibility = View.VISIBLE
                mBinding.recyclerView.visibility=View.GONE
            }
        }


    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //跳转发布需求
            R.id.tv_add -> {
                RouteManager.Mine.intentToConstructionNeedsAdd()
            }
        }
    }


}